﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ShakeIt_.ServiceReference2;
using System.ComponentModel;

namespace ShakeIt_
{
    public partial class commentPage : PhoneApplicationPage
    {
        private _UserComment _firstComment;
        public _UserComment FirstComment
        {
            set
            {
                if (_firstComment != value)
                {
                    _firstComment = value;
                    NotifyPropertyChanged("FirstComment");
                }
            }
            get
            {
                return _firstComment;
            }
        }

        public commentPage()
        {
            InitializeComponent();
            /*总评论数 Data.commentResearch.Count
             *平均得分 Data.scoreResearch
             *最新评论 Data.GetRecentComent() 返回值类型 _UserComment
             *           如果返回null，说明该用户没有评价。
             *查看更多按钮跳转 --> commentListPage.xaml
             *添加评论： Data.proxy.InsertCommentAsync(Data.userFocusedId, Data.userId, score, commentContent);
             *Data.userFocusedId 是当前被评论者
             *Data.userId 是当前评论者
             *注意在添加评论时，应有约束：Data.userFocusedId != Data.userId
            */
            Data.proxy.GetAllCommentCompleted += new EventHandler<GetAllCommentCompletedEventArgs>(GetAllCommentCompleted);
            Data.proxy.InsertCommentCompleted += new EventHandler<InsertCommentCompletedEventArgs>(InsertCommentCompleted);
        }

        private void GetAllCommentCompleted(object sender, GetAllCommentCompletedEventArgs e)
        {
            PageTitle.Text = Data.userFocusedName + "的评论";
            textBlock1.Text = "评价总数：" + Data.commentResearch.Count + "次";
            textBlock2.Text = "平均得分：" + Data.scoreResearch;
            FirstComment = Data.GetRecentComment();

            if (FirstComment == null)
            {
                textBlock7.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                textBlock7.Visibility = System.Windows.Visibility.Collapsed;
                grid1.DataContext = FirstComment;
            }

            if (Data.userFocusedId != Data.userId)
            {
                textBox1.Visibility = System.Windows.Visibility.Visible;
                button2.Visibility = System.Windows.Visibility.Visible;
                listPicker1.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                textBox1.Visibility = System.Windows.Visibility.Collapsed;
                button2.Visibility = System.Windows.Visibility.Collapsed;
                listPicker1.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/commentListPage.xaml", UriKind.Relative));
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            PageTitle.Text = Data.userFocusedName + "的评论";
            textBlock1.Text = "评价总数：" + Data.commentResearch.Count + "次";
            textBlock2.Text = "平均得分：" + Data.scoreResearch;
            FirstComment = Data.GetRecentComment();

            if (FirstComment == null)
            {
                textBlock7.Visibility = System.Windows.Visibility.Visible;
                grid1.Visibility = System.Windows.Visibility.Collapsed;
            }else
            {
                textBlock7.Visibility = System.Windows.Visibility.Collapsed;
                grid1.Visibility = System.Windows.Visibility.Visible;
                grid1.DataContext = FirstComment;
            }

            if (Data.userFocusedId != Data.userId)
            {
                textBox1.Visibility = System.Windows.Visibility.Visible;
                button2.Visibility = System.Windows.Visibility.Visible;
                listPicker1.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                textBox1.Visibility = System.Windows.Visibility.Collapsed;
                button2.Visibility = System.Windows.Visibility.Collapsed;
                listPicker1.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text != "")
            {
                Data.proxy.InsertCommentAsync(Data.userFocusedId, Data.userId, listPicker1.SelectedIndex + 1, textBox1.Text);
            }
        }

        private void InsertCommentCompleted(object sender, InsertCommentCompletedEventArgs e)
        {
            Data.proxy.GetAllCommentAsync(Data.userFocusedId);
        }
    }

    public partial class commentPage : INotifyPropertyChanged
    {
        #region Property Changed

        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        #endregion
    }
}